#define _CRT_SECURE_NO_WARNINGS 1

#include "stack.h"

void InitStack(st** pst)
{
	st* newst = (st*)malloc(sizeof(st));
	(*pst) = newst;
	(*pst)->_data = NULL;
	(*pst)->_size = (*pst)->_capacity = 0;
}

void Push(st** pst, char data)
{
	if (IsFull(pst))
	{
		size_t newcapacity = (*pst)->_capacity == 0 ? 4 : (*pst)->_capacity * 2;
		char* tmp = (char*)realloc((*pst)->_data, sizeof(char));
		(*pst)->_capacity = newcapacity;
		(*pst)->_data = tmp;
	}
	(*pst)->_data[(*pst)->_size++] = data;
}

void Pop(st** pst)
{
	assert(*pst);
	if (IsEmpty(pst)) return;
	(*pst)->_size--;
}

char Top(st** pst)
{
	assert(*pst);
	if (IsEmpty(pst))
		return;
	return (*pst)->_data[(*pst)->_size - 1];
}

bool IsEmpty(st** pst)
{
	assert(*pst);
	return (*pst)->_size == 0;
}

bool IsFull(st** pst)
{
	assert(*pst);
	return (*pst)->_capacity == (*pst)->_size;
}